package com.szrt.listener;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.szrt.entity.UserEntity;

public class HelloSessionListener implements HttpSessionListener{
	
	private static int counter = 0;
	
	
	//全部公用唯一list
	private static List<UserEntity> list = new ArrayList<UserEntity>();
	
	
	
	
	
	public static List<UserEntity> getList() {
		return list;
	}

	public static void setList(List<UserEntity> list) {
		HelloSessionListener.list = list;
	}

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		counter++;
		HttpSession session =se.getSession();
		
		System.out.println(session.getId());
		
		
		session.setAttribute("counter", counter);
		
		//UserEntity user =(UserEntity) session.getAttribute("user");
		//list.add(user);
		
		//System.out.println(list.size());
		
		//se.getSession().setAttribute("list", list);
		
		se.getSession().getServletContext().setAttribute("counterServletContext", counter);
		System.out.println("当前用户："+counter);
		
	}

	public static int getCounter() {
		return counter;
	}

	public static void setCounter(int counter) {
		HelloSessionListener.counter = counter;
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		counter--;
		se.getSession().setAttribute("counter", counter);
		
		//list删除一个
		System.out.println("当前用户："+counter);
	}

}
